package bg.consulting.labo.util;

import java.io.File;
import java.net.URL;
import java.util.Vector;


import javax.media.Codec;
import javax.media.Format;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NotRealizedError;
import javax.media.Player;
import javax.media.PlugInManager;
import javax.media.Processor;
import javax.media.ProcessorModel;
import javax.media.control.TrackControl;
import javax.media.format.AudioFormat;
import javax.media.protocol.ContentDescriptor;
import javax.media.protocol.DataSource;
import javax.media.protocol.FileTypeDescriptor;



public class MainTestPlugInMP3 {

	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		testMP3PluginAvailable();
	}
		/**
		 * @param args
		 */
	public static void main_1(String[] args) throws Exception{
		File file = new File("C:\\musique\\test.mp3");
		System.out.println(""+file.getAbsolutePath()+"  exists: "+file.exists());
		String nameUrl = ""+file.toURI();
		MediaLocator mediaLocator = new MediaLocator(nameUrl);
		System.out.println("Medialocator "+mediaLocator.getURL());
		Player player = Manager.createPlayer(mediaLocator);
		player.realize();
		player.start();
	}

	/**
	 * Permet de savoir si le codec Mp3 est disponible
	 */
	public static boolean testMP3PluginAvailable() {
		try {
			File file = new File("C:\\musique\\test.mp3");
			String nameUrl = ""+file.toURI();
			URL url =MainTestPlugInMP3.class.getClassLoader().getResource("ressources/lion_roar.mp3");
			MediaLocator mediaLocator = new MediaLocator(url);
			DataSource source_0 = Manager.createDataSource(mediaLocator);
			// Je fixe le format (de sortie) que je veux obtenir. 
			Format[] formatsArray = {new AudioFormat(AudioFormat.LINEAR)};
			ContentDescriptor contentDescriptor = new ContentDescriptor(FileTypeDescriptor.WAVE);
			
			
			Processor p = Manager.createProcessor(source_0);
			p.configure();
			
			UtilJMF.waitUntil(p,Processor.Configured);
			System.out.println("TrackControls().length  "+p.getTrackControls().length);
			TrackControl trackControl = p.getTrackControls()[0];
			Format format =trackControl.getFormat();
			Format fomataLineaire = new AudioFormat(AudioFormat.LINEAR);
			System.out.println("Format : encoding: "+format.getEncoding()+"  dataType: "+format.getDataType());
			Codec  codec = new com.sun.media.codec.audio.mp3.JavaDecoder();
			
			trackControl.setFormat(fomataLineaire);
			Codec[] codecArray = new Codec[1];
			codecArray[0] = codec;
			trackControl.setCodecChain(codecArray);
			System.out.println("Configured");
			p.realize();
			p.start();
			UtilJMF.waitUntil(p, Processor.Realized);
			DataSource source_1 = p.getDataOutput();
			Player player = Manager.createPlayer(source_1);
			player.realize();
			player.start();
			/*player.stop();
			p.stop();
			player.deallocate();
			p.deallocate();*/
			
			return true;
		} catch (Throwable e) {
			e.printStackTrace();
			return false;
		}
	}

	
	

}
